<?php
require_once('Extension/Controller/Helper/Abstract.php');

require_once('Zend/Filter/Interface.php');

class Extension_Controller_Helper_Web_FaV extends Extension_Controller_Helper_Abstract implements Zend_Filter_Interface, Zend_Validate_Interface
{
    protected $_filter = null;

    protected $_validator = null;

    public function init()
    {
        require_once('Zend/Filter.php');
        $this->_filter = new Zend_Filter();
    }

    public function filter($data)
    {
        return $data;
    }

    public function isValid($value)
    {
        return true;
    }

    public function getMessages()
    {
        return '';
    }

    public function getErrors()
    {
        return '';
    }

    public function preDispatch()
    {
    }

    public function postDispatch()
    {
    }

    public function filterPostEditData($data)
    {
        $filters = array(
            'id' => array('Digits', 'Int')
        );
        
        $validators = array(
            'id' => array('Int', Zend_Filter_Input::ALLOW_EMPTY => false)
        );
        
        $input = new Zend_Filter_Input($filters, $validators);
        
        $input->setData($data);
        
        $messages = $input->getMessages();

        $result = '';
        
        if ($input->isValid()) {
            $result = true;
        } else {
            foreach ($messages as $name => $m) {
               $result .= $name . ' : ' . implode(',', $m) . '<br />';
            }
        }
        
        return $result;
    }

    public function filterPostSaveData($data)
    {
        $filterInt = new Zend_Filter_Int();
        $filterStringTrim = new Zend_Filter_StringTrim();

        $filters = array(
            'theId'          => array($filterInt, 
                                      Zend_Filter_Input::FIELDS => 'post_id'),
            'post_title'     => $filterStringTrim,
            'post_content'   => $filterStringTrim,
        );

        $validateAlpha = new Zend_Validate_Alpha();
        $validateDigits = new Zend_Validate_Digits();
        $validateInt = new Zend_Validate_Int();
        $validateBetween = new Zend_Validate_Between(0, 10);
        $validateLessThan = new Zend_Validate_LessThan(10);
        
        $validateChainPostId = new Zend_Validate();
        $validateChainPostId->addValidator($validateDigits, true);
        $validateChainPostId->addValidator($validateInt, true);
        $validateChainPostId->addValidator($validateBetween, true);

        $validators = array(
            'post_id'        => array(Zend_Filter_Input::ALLOW_EMPTY => true, 
                                      Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED, 
                                      $validateChainPostId,
                                      Zend_Filter_Input::BREAK_CHAIN => true,
                                      ),
            
            'post_title'     => array(Zend_Filter_Input::ALLOW_EMPTY => false, 
                                      Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED, 
                                      $validateAlpha,
                                      Zend_Filter_Input::BREAK_CHAIN => false,
                                      Zend_Filter_Input::MESSAGES => 'the post title must not be empty !!',
                                      ),

            'post_content'   => array(Zend_Filter_Input::ALLOW_EMPTY => false,
                                      Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED,
                                      Zend_Filter_Input::BREAK_CHAIN => false,
                                      Zend_Filter_Input::MESSAGES => 'the post content must not be empty !!',
                                      ),

            'required_token'  => array(Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL,
                                       Zend_Filter_Input::DEFAULT_VALUE => 'token',  
                                       $validateLessThan,
                                       Zend_Filter_Input::FIELDS => 'post_id',
                                       Zend_Filter_Input::BREAK_CHAIN => true,
                                       ),
        );

        $input = new Zend_Filter_Input($filters, $validators);

        $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim());

        $options = array(Zend_Filter_Input::MISSING_MESSAGE => "Field '%field%' is required !!",
                         Zend_Filter_Input::NOT_EMPTY_MESSAGE => "Field '%field%' should not be empty !!",
                         );

        $input->setData($data);
        $input->setOptions($options);

        $messages = $input->getMessages();

        $result = '';
        
        if ($input->isValid()) {
            //var_dump($input->getEscaped('post_content'));die;
            $result = true;
        } elseif ($input->hasUnknown()) {
            $messages = $input->getUnknown();
            foreach ($messages as $name => $m) {
                $result .= $name . ' (unknown) : ' . implode(',', $m) . '<br />';
            }
        }
        
        if ($input->hasInvalid()) {
            $messages = $input->getInvalid();
            foreach ($messages as $name => $m) {
                $result .= $name . ' (invalid) : ' . implode(',', $m) . '<br />';
            }
        }
        
        if ($input->hasMissing()) {
            $messages = $input->getMissing();
            foreach ($messages as $name => $m) {
                $result .= $name . ' (missing) : ' . implode(',', $m) . '<br />';
            }
        }
        
        return $result;
    }

    public function filterAlnum($data)
    {
        $alnum = new Zend_Filter_Alnum();
        return $alnum->filter($data);
    }
    
    public function filterAlpha($data)
    {
        //$alpha = new Zend_Filter_Alpha();
        //return $alpha->filter($data);

        return Zend_Filter::get($data, 'Alpha');
    }

    public function filterBaseName($data)
    {
        $baseName = new Zend_Filter_BaseName();
        return $baseName->filter($data);
    }

    public function filterDigits($data)
    {
        $digits = new Zend_Filter_Digits();
        return $digits->filter($data);
    }

    public function filterDir($data)
    {
        $dir = new Zend_Filter_Dir();
        return $dir->filter($data);
    }

    public function filterHtmlEntities($data)
    {
        $htmlEntities = new Zend_Filter_HtmlEntities();
        return $htmlEntities->filter($data);
    }

    public function filterInt($data)
    {
        $int = new Zend_Filter_Int();
        return $int->filter($data);
    }

    public function filterRealPath($data)
    {
        $realPath = new Zend_Filter_RealPath();
        return $realPath->filter($data);
    }

    public function filterStringToLower($data)
    {
        $stringToLower = new Zend_Filter_StringToLower();
        return $stringToLower->filter($data);
    }

    public function filterStringToUpper($data)
    {
        $stringToUpper = new Zend_Filter_StringToUpper();
        $alpha = new Zend_Filter_Alpha();
        $filter = new Zend_Filter();
        $filter->addFilter($alpha);
        $filter->addFilter($stringToUpper);
        return $filter->filter($data);
    }

    public function filterStringTrim($data)
    {
        $stringTrim = new Zend_Filter_StringTrim();
        return $stringTrim->filter($data);
    }

    public function filterStripTags($data, $tagsAllowed = null, $attributesAllowed = null, $commentsAllowed = false)
    {
        $stripTags = new Zend_Filter_StripTags($tagsAllowed, $attributesAllowed, $commentsAllowed);
        return $stripTags->filter($data);
    }


}